using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace firstwinform
{
    public partial class Form1 : Form
    {
        public string text1, text2;

        public Form1()
        {
            InitializeComponent();
        }

        // 选择文件并显示内容
        private void SelectFileAndDisplayContent(TextBox textBox)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件|*.txt";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open))
                    {
                        using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                        {
                            string fileContent = sr.ReadToEnd();
                            textBox.Text = fileContent;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        // 点击文件1按钮时执行的方法
        private void file1Click(object sender, EventArgs e)
        {
            SelectFileAndDisplayContent(textBox1);
        }

        // 点击文件2按钮时执行的方法
        private void file2Click(object sender, EventArgs e)
        {
            SelectFileAndDisplayContent(textBox2);
        }

        // 合并文件按钮点击时执行的方法
        private void combineFile(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
            {
                textBox3.Clear(); // 清空第三个文本框
                textBox3.Text = textBox1.Text + textBox2.Text; // 将textBox1和textBox2的内容合并到textBox3

                SaveMergedFile();
            }
            else
            {
                MessageBox.Show("Please select both files to combine.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 保存合并后的文件
        private void SaveMergedFile()
        {
            try
            {
                string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "Data");
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath); // 创建Data子文件夹
                }

                string filePath = Path.Combine(directoryPath, "FileCom.txt");
                File.WriteAllText(filePath, textBox3.Text, Encoding.Default); // 将合并后的文本写入FileCom.txt
                MessageBox.Show("File saved successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

